package com.cloud.cloudBatis.session;

import com.cloud.cloudBatis.config.Configuration;
import com.cloud.cloudBatis.config.JdbcProperties;
import com.cloud.cloudBatis.config.MapperStatement;
import com.cloud.cloudBatis.session.impl.DefaultSqlSessionFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.Properties;

public class SqlSessionFactoryBuilder {
    public SqlSessionFactory build(String config) {
        Configuration configuration = new Configuration();
        SAXReader reader = new SAXReader();
        try {
            InputStream ins = this.getClass().getClassLoader().getResourceAsStream(config);
            Document document = reader.read(ins).getDocument();
            Element rootElement = document.getRootElement();
            String propertiesPath = rootElement.element("properties").attributeValue("resource");
            loadProperties(configuration, propertiesPath);
            for (Element element : rootElement.element("mappers").elements()) {
                String mapperPath = element.attributeValue("resource");
                loadMapper(configuration, mapperPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new DefaultSqlSessionFactory(configuration);
    }

    private void loadProperties(Configuration configuration, String propertiesPath){
        InputStream ins = this.getClass().getClassLoader().getResourceAsStream(propertiesPath);
        Properties properties = new Properties();
        try {
            properties.load(ins);
            JdbcProperties jdbcProperties = new JdbcProperties();
            jdbcProperties.setUrl(properties.getProperty("url"));
            jdbcProperties.setDriver(properties.getProperty("driver"));
            jdbcProperties.setPassword(properties.getProperty("password"));
            jdbcProperties.setUserName(properties.getProperty("username"));
            configuration.setJdbcProperties(jdbcProperties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadMapper(Configuration configuration, String mapperPath){
        SAXReader reader = new SAXReader();
        InputStream ins = this.getClass().getClassLoader().getResourceAsStream(mapperPath);
        try {
            Document document = reader.read(ins).getDocument();
            Element rootElement = document.getRootElement();
            String nameSpace = rootElement.attributeValue("namespace");
            for (Element element : rootElement.elements()) {
                String id = element.attributeValue("id");
                String resultType = element.attributeValue("resultType");
                String sql = element.getStringValue();
                MapperStatement mapperStatement = new MapperStatement();
                mapperStatement.setId(nameSpace + "." + id);
                mapperStatement.setResultType(resultType);
                mapperStatement.setSql(sql);
                configuration.getMapperMap().put(nameSpace + "." + id, mapperStatement);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
